TC_ALBUM_CONFIG = {
	TYPE:'',
	BINDING_URI: base_url + 'cms/cp/setmenu/bindingbytype/',
	IMAGE_BINDING_URI: base_url + 'cms/cp/image/bindingimagebyalbumid/',
	EDITPANEL_URI:base_url + 'cms/cp/setmenu/editpanel/',
	IMAGE_EDITPANEL_URI:base_url + 'cms/cp/image/thucdoneditpanel/',
	COMMIT_URI:base_url + 'cms/cp/setmenu/oncommit/',
	IMAGE_COMMIT_URI:base_url + 'cms/cp/image/oncommit/',
	DELETE_URI:base_url + 'cms/cp/setmenu/ondelete/',
	IMAGE_DELETE_URI:base_url + 'cms/cp/image/ondelete/'
};
var TC_ALBUM = (function() {
	
    var oTable,dtable;
    var that = this;
    return {
		onCommit: function(Url,Params, Id,callback) {
            /* Commit data lên vào DB, Id>0 là Update, Id=0 ho?c ko có là Insert */
            backend({
                url: Url,
                data: {
                    Id: Id,
                    Params: Params
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {sticky: true, theme: 'growl-error', header: 'Error!'});
                    } else {
                        $.jGrowl(rsdata.message);
                        if (typeof callback == 'function') {
                            callback(rsdata);
                        }
                    }
                }
            }).call();
        },
        createtable :   function(){
             oTable = $('#data_table').dataTable({
                aaSorting: [],
                aoColumns: [
                    {
                        mData: "album_id",sWidth: "60px",sClass:'gridAction',
                        mRender: function ( value, type, datarow ) {
							var str = 
							'<ul class="table-controls">'+
								//'<li><a href="#" class="tip" onclick="" title="Add entry" ><i class="icon-plus"></i></a> </li>'+
								'<li><a href="#" class="tip" onclick="TC_ALBUM.onEditItem(' + value + ')" title="Edit entry" ><i class="icon-pencil"></i></a> </li>';
							if(  datarow.album_lock!=1){
                                str += '<li><a href="#" class="tip" onclick="TC_ALBUM.onDeleteItem(' + value + ')" title="Remove entry" ><i class="icon-trash"></i></a> </li>';
                            }
                            str += '</ul>';
                            return str;
                        }
                    },
					{mData: "album_thumb",sWidth: "40px",sClass:'gridThumb',
						mRender: function ( value, type, datarow ) {
							if((!value || /^\s*$/.test(value))) return '';
							if(value.startsWith('/data/image/'))value=value.replace('/data/image/','/data/thumbs/image/');
                            return '<img src="' + value + '" title="Thumb"/>';
                            
                        }
					},
                    {mData: "album_title"},
                    {mData: "album_position",sWidth: "60px"},
                    {mData: "album_status",sWidth: "60px",sClass:'gridStatus',
						mRender: function ( value, type, datarow ) {
							var str=
								'<div>'+
									'<div onclick="TC_ALBUM.onStatusChange(this,'+datarow.album_id+')" class="icheckbox'+(value==="true"?" checked":"")+'"></div>'+
									'<span>'+(value==="true"?"Enable":"Disable")+'</span>'
								'</div>';
							return str;
						}
					},
                    {mData: "album_insert",sWidth: "120px"},
                    {mData: "album_update",sWidth: "120px"},
                    {mData: "album_lock",bVisible:false}
                ],
                sServerMethod: "POST",
                iDisplayLength: 10,
                bProcessing: true,
                bServerSide: true,
                bPaginate: true,
                sAjaxSource: TC_ALBUM_CONFIG.BINDING_URI+TC_ALBUM_CONFIG.TYPE,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {"sFirst": "First", "sLast": "Last", "sNext": ">", "sPrevious": "<"}
                }
            });
            oTable.fnSetFilteringDelay(2000);
        },
        onInit  :   function(){
            this.createtable();
        },
        onAddItem:function(){
            this.onEditItem(0);
        },
        onEditItem:function(Id){
            if (PENDING)
                return false;
            $('#dialog-edit-panel>.base-dialog-modal').html('<div class="base_loading" style="display: block;height: 60px"></div>');
            bckdialog({
                title:'Add/Edit Set Menu',
                message:$('#dialog-edit-panel'),
                buttons: [
                    {
                        text    :'Hoàn Thành',
                        'class'   :'base_button base_green base-small-border-radius',
                        click   : function() {
                            TC_ALBUM.onSave();
                        }
                    },
                    {
                        text    :'Đóng',
                        'class'   :'base_button base_red base-small-border-radius',
                        click   : function() {
                            $(this).dialog("close");
                        }
                    }
                ]
            }).open();
            backend({
                url         :   TC_ALBUM_CONFIG.EDITPANEL_URI+TC_ALBUM_CONFIG.TYPE,
                data        :   {
                    Id  :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        $('#dialog-edit-panel>.base-dialog-modal').html('<span style="color:red">'+rsdata.message+'</span>');
                    }else{
                        $('#dialog-edit-panel>.base-dialog-modal').html(rsdata.htmlreponse);
						$('#_form_edit_panel select').select2({minimumResultsForSearch :-1});
                        $('#dialog-edit-panel').dialog('close').dialog('open'); 
						TC_ALBUM.loadAlbumImage();
                    }
                }
            }).call();
        },
        onSave:function(){
			
            if( $('#_form_edit_panel').validationEngine('validate') === false)return false;
            var Params=$('#_form_edit_panel').serializeObject();
            $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').show();
			this.onCommit(TC_ALBUM_CONFIG.COMMIT_URI,Params,$('#album_id').val(),function(rsdata){
                $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').hide();
				if(rsdata.result<0){
					bckdialog({type:'error'}).open(rsdata.message);
				}else{
					TC_ALBUM.onRefresh();
					TC_ALBUM.onCancel();
				}
            });
            
            return false;
        },
        onCancel:function(){
            $('#dialog-edit-panel').dialog('close');
        },
        onDeleteItem:function(Id){
            backend_delete(TC_ALBUM_CONFIG.DELETE_URI,Id,function(){
                TC_ALBUM.onRefresh();
            });
        },
        onRefresh:function(){
            oTable.fnDraw(false);
        },
        onFillter:function(v){
            oTable.fnFilter(v, 2);
        },
        onStatusChange:function(that,Id){
            var status = $(that).hasClass('checked')?'false':'true';
            this.onCommit( TC_ALBUM_CONFIG.COMMIT_URI, {album_status:status}, Id, function(rsdata){
				if(rsdata.result<0){
					bckdialog({type:'error'}).open(rsdata.message);
				}else{
					$(that).toggleClass('checked');
					$(that).next().html(status=='false'?'Disable':'Enable')
				}
            });
        },
		onChangeSource : function(type){
			if(type!==TC_ALBUM_CONFIG.TYPE){
				TC_ALBUM_CONFIG.TYPE=type;
				oTable.fnSettings().sAjaxSource = (TC_ALBUM_CONFIG.BINDING_URI+TC_ALBUM_CONFIG.TYPE);
				oTable.fnDraw(false);
			}
		},
		loadAlbumImage:function(){
			dTable = $('#album_photo').dataTable({
                aaSorting: [],
                aoColumns: [
                    {
                        mData: "image_id",sWidth: "60px",sClass:'gridAction',
                        mRender: function ( value, type, datarow ) {
                            var str = 
							'<ul class="table-controls">'+
								//'<li><a href="#" class="tip" onclick="" title="Add entry" ><i class="icon-plus"></i></a> </li>'+
								'<li><a href="#" class="tip" onclick="TC_ALBUM.onEditImage(' + value + ')" title="Edit entry" ><i class="icon-pencil"></i></a> </li>'+
								'<li><a href="#" class="tip" onclick="TC_ALBUM.onDeleteImage(' + value + ')" title="Remove entry" ><i class="icon-trash"></i></a> </li>'+
                            '</ul>';
                            return str;
                        }
                    },
                    {mData: "image_source",sWidth: "40px",sClass:'gridThumb',
						mRender: function ( value, type, datarow ) {
							if((!value || /^\s*$/.test(value))) return '';
							if(value.startsWith('/data/image/'))value=value.replace('/data/image/','/data/thumbs/image/');
                            return '<img src="' + value + '" title="Thumb"/>';
							
                        }
					},
                    {mData: "image_title"},
                    {mData: "image_position",sWidth: "40px"},
                    {mData: "image_status",sWidth: "60px",sClass:'gridStatus',
						mRender: function ( value, type, datarow ) {
							var str=
								'<div>'+
									'<div onclick="TC_ALBUM.onImageStatusChange(this,'+datarow.image_id+')" class="icheckbox'+(value==="true"?" checked":"")+'"></div>'+
									'<span>'+(value==="true"?"Enable":"Disable")+'</span>'
								'</div>';
							return str;
						}
					}
                ],
                sServerMethod: "POST",
                iDisplayLength: 5,
				bFilter:false,
				bInfo:true,
				bLengthChange:false,
                bProcessing: true,
                bServerSide: true,
                bPaginate: true,
                sAjaxSource: TC_ALBUM_CONFIG.IMAGE_BINDING_URI+$('#album_code').val(),
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {"sFirst": "First", "sLast": "Last", "sNext": ">", "sPrevious": "<"}
                }
            });
		},
		onAddImage:function(){
            this.onEditImage(0);
        },
        onEditImage:function(Id){
            if (PENDING)
                return false;
            $('#image-dialog-edit-panel>.base-dialog-modal').html('<div class="base_loading" style="display: block;height: 60px"></div>');
            bckdialog({
                title:'Add/Edit Thực đơn',
                message:$('#image-dialog-edit-panel'),
                buttons: [
                    {
                        text    :'Hoàn thành',
                        'class'   :'base_button base_green base-small-border-radius',
                        click   : function() {
                            TC_ALBUM.onSaveImage();
                        }
                    },
                    {
                        text    :'Đóng',
                        'class'   :'base_button base_red base-small-border-radius',
                        click   : function() {
                            $(this).dialog("close");
                        }
                    }
                ]
            }).open();
            backend({
                url         :   TC_ALBUM_CONFIG.IMAGE_EDITPANEL_URI+$('#album_code').val(),
                data        :   {
                    Id  :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        $('#image-dialog-edit-panel>.base-dialog-modal').html('<span style="color:red">'+rsdata.message+'</span>');
                    }else{
                        $('#image-dialog-edit-panel>.base-dialog-modal').html(rsdata.htmlreponse);
						$('#image_form_edit_panel select').select2({minimumResultsForSearch :-1});
                        $('#image-dialog-edit-panel').dialog('close').dialog('open'); 
						
                    }
                }
            }).call();
        },
		onSaveImage:function(){
			
            if( $('#image_form_edit_panel').validationEngine('validate') === false)return false;
            var Params=$('#image_form_edit_panel').serializeObject();
            $("#image-dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').show();
			this.onCommit(TC_ALBUM_CONFIG.IMAGE_COMMIT_URI,Params,$('#ImageId').val(),function(rsdata){
                $("#image-dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').hide();
				TC_ALBUM.onImageRefresh();
				TC_ALBUM.onCancelImage();
            });
            
            return false;
        },
        onCancelImage:function(){
            $('#image-dialog-edit-panel').dialog('close');
        },
        onDeleteImage:function(Id){
            backend_delete(TC_ALBUM_CONFIG.IMAGE_DELETE_URI,Id,function(){
                TC_ALBUM.onImageRefresh();
            });
        },
        onImageRefresh:function(){
            dTable.fnDraw(false);
        },
		onImageStatusChange:function(that,Id){
            var status = $(that).hasClass('checked')?'false':'true';
            this.onCommit( TC_ALBUM_CONFIG.IMAGE_COMMIT_URI, {image_status:status}, Id, function(rsdata){
				if(rsdata.result<0){
					bckdialog({type:'error'}).open(rsdata.message);
				}else{
					$(that).toggleClass('checked');
					$(that).next().html(status=='false'?'Disable':'Enable')
				}
            });
        },
    };
}());